import 'package:bitcoin_mine/common/global.dart';
import 'package:bitcoin_mine/controllers/animation_controller.dart'
    as balance_animation;
import 'package:bitcoin_mine/controllers/contract_service_controller.dart';
import 'package:bitcoin_mine/services/token_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../controllers/privacy_controller.dart';
import '../widgets/privacy_agreement_dialog.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    Get.put(balance_animation.BalanceAnimationController());

    Get.put(TokenService());
    _checkPrivacyAndNavigate();
  }

  final PrivacyController privacyController = Get.put(PrivacyController());
  Future<void> _checkPrivacyAndNavigate() async {
    // 等待隐私协议检查完成
    await Future.delayed(const Duration(milliseconds: 500));

    // 如果用户已同意隐私协议，直接跳转到主页面
    if (privacyController.hasAgreedToPrivacy.value) {
      await Global.initApp();
    }
    // 如果用户未同意，保持在闪屏页显示隐私协议弹窗
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFF0A0E27),
      body: Stack(
        children: [
          // 闪屏背景
          Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // 应用图标
                Container(
                  width: 120.w,
                  height: 120.w,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(20.r),
                    gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      colors: [
                        const Color(0xFF00FFFF),
                        const Color(0xFF0099CC),
                      ],
                    ),
                    boxShadow: [
                      BoxShadow(
                        color: const Color(0xFF00FFFF).withOpacity(0.3),
                        blurRadius: 20,
                        spreadRadius: 5,
                      ),
                    ],
                  ),
                  child: Icon(Icons.diamond, color: Colors.white, size: 60.sp),
                ),

                SizedBox(height: 30.h),

                // 应用名称
                Text(
                  'Bitcoin Mine',
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),

                SizedBox(height: 10.h),

                // 副标题
                Text(
                  'Mining Your Future',
                  style: TextStyle(color: Colors.white70, fontSize: 16.sp),
                ),

                SizedBox(height: 40.h),

                // 加载指示器
                Obx(() {
                  if (privacyController.hasAgreedToPrivacy.value) {
                    return const SizedBox.shrink();
                  }
                  return Column(
                    children: [
                      CircularProgressIndicator(
                        color: const Color(0xFF00FFFF),
                        strokeWidth: 3,
                      ),
                      SizedBox(height: 20.h),
                      Text(
                        'Please agree to our privacy policy to continue',
                        style: TextStyle(
                          color: Colors.white70,
                          fontSize: 14.sp,
                        ),
                        textAlign: TextAlign.center,
                      ),
                    ],
                  );
                }),
              ],
            ),
          ),

          // 隐私协议弹窗
          Obx(() {
            if (privacyController.hasAgreedToPrivacy.value) {
              Global.initApp();
              return const SizedBox.shrink();
            }
            return const PrivacyAgreementDialog();
          }),
        ],
      ),
    );
  }
}
